package com.cynteka.cyfoman.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Sergey
 * Date: 26.07.11
 * Time: 11:40
 */
public class EventDispatcher {

    private Map<String, List<IEventListener>> listeners = new HashMap<String, List<IEventListener>>();

    public Map<String, List<IEventListener>> getListeners() {
        return listeners;
    }

    public void setListeners(Map<String, List<IEventListener>> listeners) {
        this.listeners = listeners;
    }

    public void sendEvent(IEvent event) {
        List<IEventListener> lst = listeners.get(event.getId());
        if (lst != null) {
            for (IEventListener listener : lst) {
                listener.handleEvent(event);
            }
        }
    }

    public void addEventListener(String eventId, IEventListener listener) {
        List<IEventListener> lst = listeners.get(eventId);
        if (lst == null) {
            lst = new ArrayList<IEventListener>();
            listeners.put(eventId, lst);
        }
        lst.add(listener);
    }

    public void removeEventListener(String eventId, IEventListener listener) {
        List<IEventListener> lst = listeners.get(eventId);
        if (lst != null) {
            lst.remove(listener);
        }
    }
}
